<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 房间模型类
 * Created by PhpStorm.
 * User: TK
 * Date: 14-8-1
 * Time: 上午11:09
 */
class Order_Orderroom_model extends CI_Model {


    private $order_room_table;
    private $sid;


    public function __construct()
    {
        parent::__construct();
        $this->sid = $GLOBALS['sid'];
        $this->order_room_table = $this->db->dbprefix("order_room");
        $this->hotel_room_table = $this->db->dbprefix("hotel_room");
    }


 


        /**
     * 获取订单房间
     * @param array $where 选择条件
     */

    public function get_room_array($where='',$fields = '*')
    {
        $this->db->select($fields);
        $this->db->order_by('id','desc');
        if(!empty($where)) $this->db->where($where, NULL, FALSE);
        $query = $this->db->get($this->order_room_table);
        return $query->result_array();
    }


    /**
     * 获取订单房间信息
     * @param array $where 选择条件
     */

    public function get_room_info_array($where='',$fields = '*',$page_size='20',$now_page='1')
    {
        $this->db->select($fields);
        $this->db->join($this->hotel_room_table, $this->hotel_room_table . ".id=$this->order_room_table.room_id", 'left');
        if (!empty($where)) $this->db->where($where, NULL, FALSE);
        $this->db->order_by($this->order_room_table . '.id', 'DESC');

        if (!$page_size && !$now_page)
        {
            $query = $this->db->get($this->order_room_table);
        }
        else
        {
            $query = $this->db->get($this->order_room_table, $page_size, ($now_page - 1) * $page_size);

        }

        return $query->result_array();
    }


    /**
     * 出发日期和结束日期筛选锁定房间
     * @param date $start
     * @param date $end
     * @return array 返回数据
     */

    public function get_locked_room_list($start, $end)
    {

        $condition = "sid = $this->sid and date >= '{$start}' and  date < '{$end}'";
        $dates = array();
        $ids = array();
        $result = $this->get_room_array($condition, "room_id,date", false, false);
        if ($result)
        {

            foreach ($result as $index => $item)
            {

                $ids[$index] = $item['room_id'];

                $dates[$item['room_id']][] = fdate($item['date']);

            }
            if ($ids) $ids = array_unique(array_values($ids));
            return array('ids' => $ids, 'dates' => $dates);
        }

        return array('ids' => array(), 'dates' => array());
    }



    /**
     * 更新数据
     * @param array $data  数组
     * @param mixed $where 查询条件 array|string
     * @return boolean 返回数据 true/false
     */
    public function edit_row($data,$where){
        if(!empty($where)) $this->db->where($where, NULL, FALSE);
        return $this->db->update($this->order_room_table,$data);
    }



    /**
     * 添加数据
     * @param array $data 数组
     * @return mixed 返回数据 false/[>0]
     */
    public function add_row($data){
        if(is_array($data)){
            if($this->db->insert($this->order_room_table,$data)){
                return $this->db->insert_id();
            }else{
                return false;
            }
        }
        return false;
    }

    /**
     * 批量添加数据
     * @param array $data 数组
     * @return mixed 返回数据 false/[>0]
     */
    public function add_batch($data){
        if(is_array($data)){
            if($this->db->insert_batch($this->order_room_table,$data)){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }




    /**
     * 查询单条记录
     * @param mixed $where 查询条件 array|string
     * @param string $field 获取到的字段
     * @return mixed 返回数据
     */
    public function get_row($where,$field='*')
    {
        if(!empty($where)) $this->db->where($where, NULL, FALSE);
        $query = $this->db->get($this->order_room_table,1);
        return $query->row_array();
    }

    /**
     * 删除内容
     * @param mixed $where 查询条件 array|string
     * @return bool 返回数据
     */
    public function delete_row($where)
    {
        if(!empty($where))return $this->db->delete($this->order_room_table,$where);
        return false;
    }

    /**
     * 获取冲突房间
     * @param   $where 查询条件 array|string
     * @return bool 返回数据
     */

    public function get_conflict_rooms($order_id,$room_ids = array(),$start_date ,$end_date)
    {

        $where = array();
        $conflict = array();

        if($room_ids){
            foreach ($room_ids as $index => $room_id)
            {
                $where[$index]  = " order_id != $order_id and room_id = $room_id and date >= '$start_date' and  date < '$end_date' "  ;
            }
        }


        $result = array() ;
        if($where){
            foreach ($where as $index => $item)
            {
                $res = $this->Orderroom_model->get_room_array($item);
                if($res){
                    $result[$index] = $res ;
                }
            }
        }

        if($result)
        {
            foreach ($result as $index => $item)
            {
                $conflict[$item[0]['room_id']] = $item ;
            }

        }

        return $conflict;
    }


    /**
     * 获取冲突房间
     * @param   $where 查询条件 array|string
     * @return bool 返回数据
     */

    public function get_conflict_orders($order_id, $room_ids = array(),
        $start_date, $end_date)
    {

        $where = array();
        $conflict = array();
        $return = array();

        if ($room_ids)
        {
            foreach ($room_ids as $index => $room_id)
            {
                $where[$index] = " order_id != $order_id and room_id = $room_id and date > '$start_date' and  date < '$end_date' ";
            }
        }


        $result = array();
        if ($where)
        {
            foreach ($where as $index => $item)
            {
                $res = $this->Orderroom_model->get_room_array($item);
                if ($res)
                {
                    $result[$index] = $res;
                }
            }
        }


        if ($result)
        {
            foreach ($result as $index => $item)
            {
                $t = array();
                if (is_array($item) && !empty($item))
                {
                    foreach ($item as $key => $value)
                    {
                        $t[$value['room_id']][$value['order_id']][] = $value;
                    }
                }
                $conflict[] = $t;
            }

            if ($conflict)
            {
                foreach ($conflict as $index => $item)
                {
                    $keys = array_keys($item);
                    $values = array_values($item);
                    $return[$keys[0]] = $values[0];
                }
            }
        }


        return $return;
    }



}

